package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.view.HemaWebView;
import com.hemaapp.yjnh.BaseFragmentActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.ClientDetails;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ShowTextPop;

import xtom.frame.util.XtomToastUtil;

/**走走露营商家详情
 * @author GuiyouZhang
 * Date：2016年1月12日 下午5:23:11
 * Email:guiyouzhang@163.com
 */
public class ZouzouCampsiteMerchantDetailActivity extends BaseFragmentActivity implements OnClickListener{

    private TextView titleText;
    private ImageButton titleLeft;
    private ImageView titleLove;
    private Button titleRight;

    private ScrollView scrollview;
    //	private FrameLayout layoutHead;//轮播图片的父布局
//	private AutoChangeViewPager vpStore;
//	private RelativeLayout layoutDots;//小圆点
    private ImageView image;
    private TextView tvMerchantName;//商家名称
    private RatingBar ratingBar;
    private TextView tvMerchantAddress;
    private TextView tvFcly;
    private TextView tvZjly;
    private TextView tvZply;
    private TextView tvGoods;
    private HemaWebView webview;
    private LinearLayout returnTop;
    private TextView tvPhone;
    private TextView tvConsultOnline;
    private TextView tv_info;
    private ImageView iv_info;

    private String client_id;//当前商家的id
    private User user;
    private ClientDetails details;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_zouzou_campsite_merchant_detail);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
        String token = user == null?"":user.getToken();
        getNetWorker().clientGet(token, client_id);
    }

    @Override
    protected void getExras() {
        client_id = mIntent.getStringExtra("client_id");
    }

    @Override
    protected void findView() {
        titleText = (TextView) findViewById(R.id.title_text);
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleRight = (Button) findViewById(R.id.title_right_btn);
        titleLove = (ImageView) findViewById(R.id.title_right_img);

        scrollview = (ScrollView) findViewById(R.id.scrollview);
        image = (ImageView) findViewById(R.id.image);
//		layoutHead = (FrameLayout) findViewById( R.id.layout_head );
//		vpStore = (AutoChangeViewPager)findViewById( R.id.vp_store );
//		layoutDots = (RelativeLayout)findViewById( R.id.layout_dots );
        tvMerchantName = (TextView)findViewById( R.id.tv_merchant_name );
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        tvMerchantAddress = (TextView)findViewById( R.id.tv_merchant_address );
        tvFcly = (TextView)findViewById( R.id.tv_fcly );
        tvZjly = (TextView)findViewById( R.id.tv_zjly );
        tvZply = (TextView)findViewById( R.id.tv_zply );
        tvGoods = (TextView)findViewById( R.id.tv_goods );
        webview = (HemaWebView) findViewById(R.id.webview);
        tvPhone = (TextView)findViewById( R.id.tv_phone );
        tvConsultOnline = (TextView)findViewById( R.id.tv_consult_online );
        tv_info = (TextView) findViewById(R.id.tv_info);
        iv_info = (ImageView) findViewById(R.id.iv_info);
        returnTop = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
    }

    @Override
    protected void setListener() {
        titleText.setText("商家详情");
        titleLeft.setOnClickListener(this);
        titleRight.setVisibility(View.GONE);
        titleLove.setVisibility(View.VISIBLE);
        titleLove.setImageResource(R.drawable.star_while_stroke);
        titleLove.setOnClickListener(this);
        returnTop.setOnClickListener(this);
        tvPhone.setOnClickListener(this);
        tvConsultOnline.setOnClickListener(this);
        tvMerchantAddress.setOnClickListener(this);
        tvFcly.setOnClickListener(this);
        tvZjly.setOnClickListener(this);
        tvZply.setOnClickListener(this);
        tvGoods.setOnClickListener(this);
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask.getHttpInformation();
        switch (information) {
            case CLIENT_GET:
                showProgressDialog("请稍候");
                break;
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_GET:
                cancelProgressDialog();
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_GET:
                HemaArrayResult<ClientDetails> uResult = (HemaArrayResult<ClientDetails>) baseResult;
                details = uResult.getObjects().get(0);
                setData();
                break;
            case FOLLOW_COLLECT_SAVEOPERATE:
                String oper = netTask.getParams().get("oper");
                // 操作	1添加 ，2取消 ;3：清空
//			if (oper.equals("1")) {
//				showTextDialog("添加关注成功!");
//				titleLove.setImageResource(R.drawable.heart_while);
//				details.setFollowflag("1");
//			}else {
//				showTextDialog("取消关注成功!");
//				titleLove.setImageResource(R.drawable.heart_while_stroke);
//				details.setFollowflag("0");
//			}
                ShowTextPop stp = new ShowTextPop(mContext,
                        findViewById(R.id.father));
                String follow = details.getFollowflag();
                if (follow.equals("0")) {//关注成功
                    details.setFollowflag("1");
                    stp.setText("已关注");
                    stp.setDrawable(R.drawable.follow, 0, 0, 0);
                    titleLove.setImageResource(R.drawable.heart_while);
                }else if (follow.equals("1")) {//取消关注
                    details.setFollowflag("0");
                    stp.setText("取消关注");
                    stp.setDrawable(R.drawable.unfollow, 0, 0, 0);
                    titleLove.setImageResource(R.drawable.heart_while_stroke);
                }
                stp.show();
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_GET:
                showTextDialog("获取商家信息失败！");
                titleText.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
                break;
            case FOLLOW_COLLECT_SAVEOPERATE:
                String oper = netTask.getParams().get("oper");
                // 操作	1添加 ，2取消 ;3：清空
                if (oper.equals("1")) {
                    showTextDialog("添加关注失败!");
                }else {
                    showTextDialog("取消关注失败!");
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case CLIENT_GET:
                showTextDialog("获取商家信息失败！");
                titleText.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
                break;
            case FOLLOW_COLLECT_SAVEOPERATE:
                String oper = netTask.getParams().get("oper");
                // 操作	1添加 ，2取消 ;3：清空
                if (oper.equals("1")) {
                    showTextDialog("添加关注失败!");
                }else {
                    showTextDialog("取消关注失败!");
                }
                break;
            default:
                break;
        }
    }

    private void setData() {
        if (details == null) {
            return;
        }
        tvMerchantName.setText(details.getNickname());
        if (!isNull(details.getContent())) {
            tv_info.setVisibility(View.VISIBLE);
            tv_info.setText(details.getContent());
        }else
            tv_info.setVisibility(View.GONE);
        ImageUtils.loadBigImage(mContext, details.getImgurl0(), iv_info);
        ImageUtils.loadBigImage(mContext, details.getBackimg(), image);
        Float score = Float.parseFloat(details.getStarscore());
        ratingBar.setRating(score);
        tvMerchantAddress.setText(details.getAddress());
        //判断关注标记	0未关注；1已关注
        String follow = details.getFollowflag();
        if (follow==null || follow.equals("0")) {
            titleLove.setImageResource(R.drawable.heart_while_stroke);
        }else {
            titleLove.setImageResource(R.drawable.heart_while);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_img:
                if (BaseUtil.isLogin()) {
                    String token = getApplicationContext().getUser().getToken();
                    if ("0".equals(details.getFollowflag())) {//未关注
                        getNetWorker().FolColOperate(token, "1", "1", details.getId());
                    }else {//已关注
                        getNetWorker().FolColOperate(token, "1",  "2", details.getId());
                    }
                } else {
                    Intent itCart = new Intent(mContext, LoginActivity.class);
                    startActivity(itCart);
                }
                break;
            case R.id.tv_merchant_address:
                Intent itAddress = new Intent(mContext, SellerMapActivity.class);
                itAddress.putExtra("merchant", details);
                startActivity(itAddress);
                break;
            case R.id.ll_return_top:
                scrollview.smoothScrollTo(0, 0);
                break;
            case R.id.tv_phone:
                final String phone = details.getMobile();
                if (phone != null && !phone.isEmpty() && !phone.equals("无")) {
                    HemaButtonDialog hbDialog = new HemaButtonDialog(mContext);
                    hbDialog.setText("是否直接拨打"+phone+"?");
                    hbDialog.setLeftButtonText("取消");
                    hbDialog.setRightButtonText("拨打");
                    hbDialog.setRightButtonTextColor(getResources().getColor(R.color.theme_orange));
                    hbDialog.setButtonListener(new HemaButtonDialog.OnButtonListener() {

                        @Override
                        public void onLeftButtonClick(HemaButtonDialog dialog) {
                            dialog.cancel();
                        }

                        @Override
                        public void onRightButtonClick(HemaButtonDialog dialog) {
                            dialog.cancel();
                            Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(
                                    "tel:" + phone));
                            startActivity(intent);
                        }
                    });
                }else {
                    showTextDialog("对方没有提供有效的电话号码");
                }
                break;
            case R.id.tv_consult_online://咨询
                if (BaseUtil.isLogin()) {
                    String mQQNum = details.getQq();
                    if (mQQNum == null || mQQNum.isEmpty() || mQQNum.equals("null")) {
                        XtomToastUtil.showShortToast(mContext, "对方没有指定QQ");
                        break;
                    }
                    String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + mQQNum;
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    } catch (Exception e) {
                        showTextDialog("打开QQ客户端失败\n请确保已经安装了QQ手机客户端");
                        e.printStackTrace();
                    }
                }else {
                    Intent itCart = new Intent(mContext, LoginActivity.class);
                    startActivity(itCart);
                }
                break;
            case R.id.tv_fcly:
                Intent it = new Intent(mContext, CmnProductionListActivity.class);
                it.putExtra("title", "房车露营");
                it.putExtra("keytype", "10");
                it.putExtra("keyid", "71");
                it.putExtra("keyword", "");
                it.putExtra("saleflag", "");
                it.putExtra("topflag", "");
                it.putExtra("cheapflag", "");
                it.putExtra("score_percent", "");
                it.putExtra("seller_id", "");
                it.putExtra("district_name", "");
                startActivity(it);
                break;
            case R.id.tv_zjly:
                Intent itZjly = new Intent(mContext, CmnProductionListActivity.class);
                itZjly.putExtra("title", "自驾露营");
                itZjly.putExtra("keytype", "10");
                itZjly.putExtra("keyid", "72");
                itZjly.putExtra("keyword", "");
                itZjly.putExtra("saleflag", "");
                itZjly.putExtra("topflag", "");
                itZjly.putExtra("cheapflag", "");
                itZjly.putExtra("score_percent", "");
                itZjly.putExtra("seller_id", "");
                itZjly.putExtra("district_name", "");
                startActivity(itZjly);
                break;
            case R.id.tv_zply:
                Intent itZply = new Intent(mContext, CmnProductionListActivity.class);
                itZply.putExtra("title", "帐篷露营");
                itZply.putExtra("keytype", "10");
                itZply.putExtra("keyid", "73");
                itZply.putExtra("keyword", "");
                itZply.putExtra("saleflag", "");
                itZply.putExtra("topflag", "");
                itZply.putExtra("cheapflag", "");
                itZply.putExtra("score_percent", "");
                itZply.putExtra("seller_id", "");
                itZply.putExtra("district_name", "");
                startActivity(itZply);
                break;
            case R.id.tv_goods:
                Intent itGoods = new Intent(mContext, CmnProductionListActivity.class);
                itGoods.putExtra("title", "商品区");
                itGoods.putExtra("keytype", "10");
                itGoods.putExtra("keyid", "74");
                itGoods.putExtra("keyword", "");
                itGoods.putExtra("saleflag", "");
                itGoods.putExtra("topflag", "");
                itGoods.putExtra("cheapflag", "");
                itGoods.putExtra("score_percent", "");
                itGoods.putExtra("seller_id", "");
                itGoods.putExtra("district_name", "");
                startActivity(itGoods);
                break;
            default:
                break;
        }
    }

}
